package logic

import (
	"context"
	"errors"
	"video-system/model/mysql"

	"video-system/userrpc/internal/svc"
	"video-system/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateNicknameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateNicknameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateNicknameLogic {
	return &UpdateNicknameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改昵称
func (l *UpdateNicknameLogic) UpdateNickname(in *userrpc.UpdateNicknameRequest) (*userrpc.UpdateNicknameResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.UserInfo

	if err := findUser.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, err
	}

	if findUser.ID == 0 {
		return nil, errors.New("用户不存在")
	}

	findUser.NickName = in.NickName

	if err := findUser.UpdateNickName(mysql.DB); err != nil {
		return nil, errors.New("修改失败")
	}

	return &userrpc.UpdateNicknameResponse{
		UserId: int64(findUser.ID),
	}, nil
}
